﻿using HBase;
using HBase.db;
using hCommand;
using Net.http;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace Persistence {
    public   class Csd2CsEngine : GkTransaction {
        public long id;
        public long pid;
        public bool pingku=false;
      
        protected override TransRes Execute() {
            CoreStockDet v = getById<CoreStockDet>(id);
            if (pid == 0) {
               return quxiao(v);
            }
            if (!BUtil.eq(v.status, StockDetailStatus.待绑定, StockDetailStatus.库存)) {
                return new TransRes("明细状态异常" + v.id + "-》" + v.status);
            }
            CoreStock cs = getById<CoreStock>(pid);
            if (cs == null) { 
                return new TransRes("托盘不存在" + pid );
            } 
            CoreItem item = getById<CoreItem>(v.itemId);
            if (item == null) {
                return new TransRes("物料不存在" + id);
            } 
            CoreWhLoc loc = getById<CoreWhLoc>(cs.locId);
            if (loc == null) {
                return new TransRes("货位不存在" + cs.locId);
            }  
            int c = queryUnique<int>("SELECT count(*) FROM core_classify_area  where  classify_id=@classifyId and area_Id=@areaId", new { item.classifyId, loc.areaId });
            if (c == 0) {
                return new TransRes("位置与物料不匹配" );
            } 
            String sql1 = "SELECT DISTINCT i.classify_Id FROM `core_stock_det` d join core_item i on d.item_Id=i.id   where status<70 and pid=@id";
            List<long> ids = query<long>(sql1, cs);
            if (ids.Count > 1) {
                return new TransRes("一个托盘多个分类" + v.id + cs.classifyId);
            } else if (ids.Count == 1) {
                if (ids[0] != item.classifyId) {
                    return new TransRes("一个托盘只能放一个分类" + v.id + ids[0]);
                }
            }
            
            if (v.pid > 0) {
                exec("update Core_Stock_Det set pid=0   where id=@id", v); 
                CsdUtil.updateCSdetCount(this, v.pid); 
            } else {
                exec(HUtil.s("update Core_Stock_Det set create_Time=sgj_get_sys_now ,doo_det_id=0 ,status={0} where id=@id", StockDetailStatus.库存), v);
                StatUtil.changeBatch(this, v, true); 
                exec("update Wh_Doi_det set  cv=cv+1    where id=@doiDetId   ", v);
            } 
            updateCSD(cs, v );
            updataCS(cs, item); 
            return new TransRes();
        }

    
        TransRes quxiao(CoreStockDet v) {
            exec(HUtil.s("update Core_Stock_Det set pid=0, doo_det_id=0 ,status={0} where id=@id", StockDetailStatus.待绑定), v);
            StatUtil.changeBatch(this, v, true);
            exec("update Wh_Doi_det set  cv=cv+1    where id=@doiDetId   ", v);
            return new TransRes("解除绑定");
        }
        void updataCS(CoreStock cs, CoreItem item) { 
            int remain = CsdUtil.countDet(this, cs.id);
            String sql = "update Core_Stock set classify_Id=@classifyId  ,det_count=@remain  ,change_Time=sgj_get_sys_now where id=@id";
            exec(sql, new { item.classifyId, id = cs.id, remain }); 
        }

        void updateCSD(CoreStock cs, CoreStockDet v ) { 
            String sql2 = "update Core_Stock_Det set pid=@pid  where id=@id";
            exec(sql2, new { pid = cs.id, id = v.id });
        }
    }
}
